package com.im.partchat;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;

import com.im.partchat.bean.UserBean;
import com.im.partchat.topic.manager.FaceManager;
import com.im.partchat.topic.manager.HomeManager;
import com.im.partchat.utils.FileUtils;
import com.im.partchat.utils.LogUtils;
import com.raizlabs.android.dbflow.config.FlowManager;

import io.rong.imageloader.core.DisplayImageOptions;
import io.rong.imageloader.core.display.FadeInBitmapDisplayer;
import io.rong.imkit.RongIM;


/**
 * Created by flyou on 2017/6/26.
 * 自定义Application
 */

public class App extends Application {

    private static App mContext;
    private static UserBean userBean;
    private static DisplayImageOptions options;
    private static FaceManager faceManager;
    private static HomeManager homeManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))) {
            RongIM.init(this);
            PartChatAppContext.init(this);
        }
        FlowManager.init(this);
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.rc_image_default)
                .showImageOnFail(R.drawable.rc_image_default)
                .showImageOnLoading(R.drawable.rc_image_default)
                .displayer(new FadeInBitmapDisplayer(300))
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        //设置是否打印日志
        LogUtils.setIsLog(BuildConfig.LOG_DEBUG);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            FileUtils.init(this);
        }

        homeManager = new HomeManager(this);
        faceManager = new FaceManager(this);
        faceManager.initFaceMap();
    }

    public static HomeManager getHomeManager() {
        return homeManager;
    }

    public static FaceManager getFaceManager() {
        return faceManager;
    }

    public static DisplayImageOptions getOptions() {
        return options;
    }

    public static App getApplication() {
        return mContext;
    }

    public static UserBean getUserBean() {
        return userBean;
    }
    public static void setUserBean(UserBean userBean){
        App.userBean = userBean;
    }

    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
